<script setup>
import { ref,onMounted,onActivated } from 'vue'
import { queryWarehousingUnload,confirmWarehousingUnload } from '../../api/warehousingentrydetails'
import { getAllUser } from '../../api/user'
import { ElMessage } from 'element-plus'

//定义userlist集合
const userList = ref([])
const getUserList = async () => { 
  const res = await getAllUser()
  userList.value = res.data
}

//定义表格数据
const warehousingUnloadList = ref([])
//查询的点击事件
const query = async ()=>{ 
  const result = await queryWarehousingUnload(page.value,pageSize.value,warehousingUnloadFrom.value.supplierName)
  warehousingUnloadList.value = result.data.rows
  total.value = result.data.total
}

//定义表单数据
const warehousingUnloadFrom = ref({ supplierName:''})
//清空表单数据
const clear = ()=>{
  warehousingUnloadFrom.value = {supplierName:''}
  query()
}
//定义弹出框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({userId:'',unloadTime:''})

//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}
//挂载
onMounted(() => {
  getUserList()
  query()
})

// 当组件被激活时重新查询数据（适用于keep-alive缓存的组件）
onActivated(() => {
   query()
})

//创建一个id集合来接收
const idsList = ref([])
//定义表格的选中事件
const handleSelectionChange = (value) => {
  idsList.value = value.map(item=>item.id)
}

//提交的点击事件
const saveWarehousingUnloadData = ()=>{ 
  // 检查是否有选中的数据
  if (idsList.value.length === 0) {
    ElMessage.warning('请先选择要操作的数据！')
    return
  }
  title.value = '卸货时间'
  dialogShow.value = true
  //清空表单
  dialogData.value = {userId:'',dateTime:'',list:[]}
}

//定义存数据的变量
const data = ref({userId:'',dateTime:'',list:[]})
//确定按钮的点击事件
const confirmeWarehousingUnloadInfo = async () => { 
  data.value.list = idsList.value
  data.value.dateTime = dialogData.value.unloadTime
  data.value.userId = dialogData.value.userId
  const result = await confirmWarehousingUnload(data.value)
  if(result.code){
    ElMessage.success('操作成功！')
    dialogShow.value = false
    idsList.value = []
    query()
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}

</script>

<template>
<div><h4>待卸货</h4></div>
<div>
  <el-form :inline="true" :model="warehousingUnloadFrom" class="demo-form-inline">
    <el-form-item>
      <el-button type="primary" @click="saveWarehousingUnloadData" circle plain><el-icon><Select /></el-icon></el-button>
    </el-form-item>
    <el-form-item>
      <el-input v-model="warehousingUnloadFrom.supplierName" placeholder="供应商名称" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<!-- table表格 -->
<div>
  <el-table :data="warehousingUnloadList" @selection-change="handleSelectionChange" border style="width: 100%" >
    <el-table-column type="selection" width="55" />
    <el-table-column fixed type="index" label="序号" align="center"  width="80"/>
    <el-table-column prop="batchNumber" label="批次号" align="center" width="120"/>
    <el-table-column prop="supplierName" label="供应商名称" align="center" width="180"/>
    <el-table-column prop="title" label="商品名称" align="center" width="200"/>
    <el-table-column prop="count" label="商品数量" align="center" width="120"/>
    <el-table-column prop="price" label="商品价格" align="center" width="120"/>
    <el-table-column prop="consignorName" label="货主名称" align="center"  width="200"/>
    <el-table-column prop="predictedArrivalTime" label="预测到达时间" align="center" width="200"/>
  </el-table>
</div>


<!-- 分页 -->
<div>
  <el-pagination
  v-model:current-page="page"
  v-model:page-size="pageSize"
  :page-sizes="[5, 10, 15, 20]"
  layout="total, sizes, prev, pager, next, jumper"
  :total="total"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"/>
</div>

<!-- 弹出框 -->
<div>
  <el-dialog v-model="dialogShow" :title="title" width="400" align-center>
    <el-form :model="dialogData">
      <el-form-item>
        <el-select v-model="dialogData.userId" placeholder="卸货员" style="width: 300px;">
          <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dialogData.unloadTime" type="date" placeholder="卸货时间" value-format="YYYY-MM-DD" style="width: 300px;" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogShow = false" type="default">取消</el-button>
      <el-button type="primary" @click="confirmeWarehousingUnloadInfo" plain>确定</el-button>
    </template>
  </el-dialog>
</div> 

</template>
<style scoped>
</style>